package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.TransportCompanyInfo;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.TransportCompanyInfoRepository;
import co.yixiang.modules.shop.service.TransportCompanyInfoService;
import co.yixiang.modules.shop.service.dto.TransportCompanyInfoDto;
import co.yixiang.modules.shop.service.dto.TransportCompanyInfoQueryCriteria;
import co.yixiang.modules.shop.service.mapper.TransportCompanyInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lijuzne
* @date 2020-05-08
*/
@Service
//@CacheConfig(cacheNames = "transportCompanyInfo")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TransportCompanyInfoServiceImpl implements TransportCompanyInfoService {

    private final TransportCompanyInfoRepository transportCompanyInfoRepository;

    private final TransportCompanyInfoMapper transportCompanyInfoMapper;

    public TransportCompanyInfoServiceImpl(TransportCompanyInfoRepository transportCompanyInfoRepository, TransportCompanyInfoMapper transportCompanyInfoMapper) {
        this.transportCompanyInfoRepository = transportCompanyInfoRepository;
        this.transportCompanyInfoMapper = transportCompanyInfoMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(TransportCompanyInfoQueryCriteria criteria, Pageable pageable){
        Page<TransportCompanyInfo> page = transportCompanyInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(transportCompanyInfoMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<TransportCompanyInfoDto> queryAll(TransportCompanyInfoQueryCriteria criteria){
        return transportCompanyInfoMapper.toDto(transportCompanyInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public TransportCompanyInfoDto findById(Integer id) {
        TransportCompanyInfo transportCompanyInfo = transportCompanyInfoRepository.findById(id).orElseGet(TransportCompanyInfo::new);
        ValidationUtil.isNull(transportCompanyInfo.getId(),"TransportCompanyInfo","id",id);
        return transportCompanyInfoMapper.toDto(transportCompanyInfo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public TransportCompanyInfoDto create(TransportCompanyInfo resources) {
        return transportCompanyInfoMapper.toDto(transportCompanyInfoRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(TransportCompanyInfo resources) {
        TransportCompanyInfo transportCompanyInfo = transportCompanyInfoRepository.findById(resources.getId()).orElseGet(TransportCompanyInfo::new);
        ValidationUtil.isNull( transportCompanyInfo.getId(),"TransportCompanyInfo","id",resources.getId());
        transportCompanyInfo.copy(resources);
        transportCompanyInfoRepository.save(transportCompanyInfo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            transportCompanyInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TransportCompanyInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TransportCompanyInfoDto transportCompanyInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", transportCompanyInfo.getUserId());
            map.put("标题", transportCompanyInfo.getTitle());
            map.put("手机号", transportCompanyInfo.getPhone());
            map.put("品牌id", transportCompanyInfo.getBrandId());
            map.put("类型id", transportCompanyInfo.getCateId());
            map.put("省", transportCompanyInfo.getProvince());
            map.put("市", transportCompanyInfo.getCity());
            map.put("区", transportCompanyInfo.getDistrict());
            map.put("具体地址", transportCompanyInfo.getDetail());
            map.put("详细信息", transportCompanyInfo.getMessage());
            map.put("图片", transportCompanyInfo.getImage());
            map.put("阅读次数", transportCompanyInfo.getCountNumber());
            map.put("创建时间", transportCompanyInfo.getCreateTime());
            map.put("是否删除", transportCompanyInfo.getDelFlag());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}